package com.bw.controller.lizeliang;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
//日期，项目经理，姓名，面试公司，上午，下午，面试前辅导录音是否提交，面试后诊断录音是否提交
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bw.bean.ActiveUser;
import com.bw.bean.Clazz;
import com.bw.bean.Interview;
import com.bw.bean.Teacher;
import com.bw.bean.lizeliang.ClassTree;
import com.bw.bean.lizeliang.ShouyeData;
import com.bw.bean.lizeliang.StudentTree;
import com.bw.service.lizeliang.DataService;
import com.bw.util.ExportExcel;
import com.bw.util.SessionUtil;

@Controller
@RequestMapping("data")
public class DataController {
	
	@Autowired
	private DataService dataServiceImpl;
	@ModelAttribute("teacherList")
	public List<Teacher> teacherList(HttpServletRequest request){
		ActiveUser user = (ActiveUser)request.getSession().getAttribute("activeUser");
		if(user.getTid() == -1){
			return dataServiceImpl.getTeacherList();
		}
		return null;
	}
	@RequestMapping("getStudents")
	@ResponseBody
	public List<ClassTree> getStudents(HttpServletRequest request){
		/*List<ClassTree> cc = new ArrayList<ClassTree>();
		ClassTree tree = new ClassTree();
		List<StudentTree> list = new ArrayList<StudentTree>();
		for (int i = 0; i < 10; i++) {
			list.add(new StudentTree(i, "筱筱"+i));
		}
		tree.setId(1);
		tree.setText("1501");
		tree.setChildren(list);
		cc.add(tree);*/
		ActiveUser user = (ActiveUser)request.getSession().getAttribute("activeUser");
		List<ClassTree> list = dataServiceImpl.getClassTree(user.getTid());
		return list;
	}
	@RequestMapping("addInterview")
	@ResponseBody
	public boolean addInterview(Interview interview){
		System.err.println(interview+"面试信息");
		return dataServiceImpl.addInterview(interview)==1;
	}
	@ModelAttribute("classList")
	public List<Clazz> list(HttpServletRequest request){
		/*List<Clazz> classList = dataServiceImpl.getClassList();*/
		ActiveUser teacher = (ActiveUser)request.getSession().getAttribute("activeUser");
		return dataServiceImpl.getClassList(teacher.getTid());
	}
	@RequestMapping("shouye")
	public String shouye(){
		return "shouye";
	}
	@RequestMapping("getInfoBySid")
	public boolean getInfoBySid(ShouyeData data){
		System.err.println(data);
		return true;
	}
	@RequestMapping("refuse")
	public String refuse(){
		System.err.println("--------------------------------");
		return "refuse";
	}
	@RequestMapping("shouyeData")
	@ResponseBody
	public List<ShouyeData> shouyeData(HttpServletRequest request){
		String sname = request.getParameter("sname");
		String cids = request.getParameter("cid");
		String tname = request.getParameter("tname");
		
		
		if(tname == null){
			tname = "";
		}
		
		
		Integer cid = null;
		if(sname == null){
			sname = "";
		}
		if(cids != null){
			cid = Integer.parseInt(cids);
		}
		String start = request.getParameter("start");
		String end = request.getParameter("end");
		if(start == ""){
			start = null;
		}
		if(end == ""){
			end = null;
		}
		ActiveUser teacher = (ActiveUser)request.getSession().getAttribute("activeUser");
		/*select.setTid(1);*/
		List<ShouyeData> list = dataServiceImpl.getData(teacher.getTid(), sname, cid, start, end, tname);
		SessionUtil.addData(request, list);
		return list;
	}
	@RequestMapping("getTable")
	public String getTable(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String fileName = "学生报表";
		/*response.reset();*/
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename="+ new String((fileName + ".xls").getBytes(), "iso-8859-1"));
        ServletOutputStream out = response.getOutputStream();
      //日期，项目经理，姓名，面试公司，上午，下午，面试前辅导录音是否提交，面试后诊断录音是否提交
		ExportExcel<ShouyeData> ex = new ExportExcel<ShouyeData>();
		String[] headers = {"编号", "学生编号", "班级", "日期", "项目经理", "姓名", "面试公司", "上午", "下午", "面试前辅导录音是否提交", "面试后诊断录音是否提交"};
		//List<StudentVo> dataset = teachersServiceImpl.getStudents(1, "2017-03-22", "2017-03-23");
		List<ShouyeData> dataset = SessionUtil.getDate(request);
		try {
			/*OutputStream out = new FileOutputStream(
					 "D:\\excel\\datas.xls");*/
			ex.exportExcel(headers, dataset, out);
			out.close();
			/*JOptionPane.showMessageDialog(null, "导出成功!");*/
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}
